﻿// <copyright file="Hello.cs" company="Ucaya">
// </copyright>
// <author>Ucaya</author>

namespace KinectExtensions.Controls
{
    #region

    using System.ComponentModel;
    using System.ComponentModel.Composition;
    using System.Windows;
    using System.Windows.Controls;

    using KinectExtensions.Helpers;
    using KinectExtensions.Services;

    #endregion

    public class Hello : Control
    {
        #region Constants and Fields

        /// <summary>
        ///   HelloGestureTracker depedency property.
        /// </summary>
        public static readonly DependencyProperty HelloGestureTrackerProperty = DependencyProperty.Register("HelloGestureTracker", typeof(HelloGestureTracker), typeof(Hello), new PropertyMetadata((d, e) => ((Hello)d).OnHelloGestureTrackerChanged((HelloGestureTracker)e.OldValue, (HelloGestureTracker)e.NewValue)));

        #endregion

        #region Constructors and Destructors

        static Hello()
        {
            DefaultStyleKeyProperty.OverrideMetadata(typeof(Hello), new FrameworkPropertyMetadata(typeof(Hello)));
        }

        public Hello()
        {
            ServiceLocator.Instance.SatisfyImportsOnce(this);
            this.HelloGestureTracker = this.HandTrackingService.HelloGestureTracker;
        }

        #endregion

        #region Public Properties

        [Import]
        public HandTrackingService HandTrackingService { get; set; }

        public HelloGestureTracker HelloGestureTracker
        {
            get
            {
                return (HelloGestureTracker)this.GetValue(HelloGestureTrackerProperty);
            }

            set
            {
                this.SetValue(HelloGestureTrackerProperty, value);
            }
        }

        #endregion

        #region Public Methods

        public override void OnApplyTemplate()
        {
            base.OnApplyTemplate();
            this.RefreshState();
        }

        #endregion

        #region Methods

        private void HelloGesturePropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            if (e.PropertyName == this.HelloGestureTracker.GetPropertyName(_ => _.IsRecognizing) || e.PropertyName == this.HelloGestureTracker.GetPropertyName(_ => _.SameSideGestureCount) || e.PropertyName == this.HelloGestureTracker.GetPropertyName(_ => _.SameSideGestureNeeded) || e.PropertyName == this.HelloGestureTracker.GetPropertyName(_ => _.CurrentXYDirection) || e.PropertyName == this.HelloGestureTracker.GetPropertyName(_ => _.IsHandReady))
            {
                this.RefreshState();
            }
        }

        /// <summary>
        ///   handles the HelloGestureTrackerProperty changes.
        /// </summary>
        /// <param name = "oldValue">The old value.</param>
        /// <param name = "newValue">The new value.</param>
        private void OnHelloGestureTrackerChanged(HelloGestureTracker oldValue, HelloGestureTracker newValue)
        {
            if (oldValue != null)
            {
                oldValue.PropertyChanged -= this.HelloGesturePropertyChanged;
            }

            if (newValue != null)
            {
                newValue.PropertyChanged += this.HelloGesturePropertyChanged;
            }
        }

        private void RefreshState()
        {
            if (this.HelloGestureTracker != null)
            {
                VisualStateManager.GoToState(this, this.HelloGestureTracker.IsHandReady ? "HandReadyState" : "NoHandState", true);
            }
            if (this.HelloGestureTracker == null || !this.HelloGestureTracker.IsRecognizing || this.HelloGestureTracker.CurrentXYDirection == null)
            {
                VisualStateManager.GoToState(this, "NoDetection", false);
            }
            else
            {
                double pc = (double)this.HelloGestureTracker.SameSideGestureCount / this.HelloGestureTracker.SameSideGestureNeeded;

                if (this.HelloGestureTracker.SameSideGestureCount >= this.HelloGestureTracker.SameSideGestureNeeded)
                {
                    if (this.HelloGestureTracker.CurrentXYDirection.Value == XyGestureDirection.NW || this.HelloGestureTracker.CurrentXYDirection.Value == XyGestureDirection.W)
                    {
                        VisualStateManager.GoToState(this, "DetectRightToLeft", false);
                    }
                    else
                    {
                        VisualStateManager.GoToState(this, "DetectLeftToRight", false);
                    }
                }
            }
        }

        #endregion
    }
}